// +build linux,!windows,!darwin,!js

package client

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/gen2brain/dlgs"
)

var RAW_PROFILE = ""

var (
	GlobalStatus = false
)

func IfProxyStart() bool {
	result := os.Getenv("http_proxy")
	if result == "" {
		if os.Getenv("HTTP_PROXY") != "" {
			return true
		}
	} else {
		return true
	}
	return false
}

func InitDesktop() {
	KCPPATH := filepath.Join(os.Getenv("GOPATH"), "bin", "Kcpee")
	DEKPATH := filepath.Join(os.Getenv("HOME"), "Desktop")
	if _, err := os.Stat(DEKPATH); err == nil {

	}

	APP_base := "/usr/share/Kcpee"
	if _, err := os.Stat(APP_base); err != nil {
		os.MkdirAll(APP_base, os.ModePerm)
	}

	PICPATH := "/usr/share/Kcpee/icon.png"
	if _, err := os.Stat(PICPATH); err != nil {
		cmd := exec.Command("wget", "-c", "-t", "5", "https://github.com/Qingluan/Kcpee/raw/master/ICON%40256x256.png", "-O", PICPATH)
		cmd.Start()
		dlgs.Info("Initing", "create Desktop app in /usr/share/applications/Kcpee.desktop")
	}
	INIT := `
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=` + KCPPATH + `
Name=Kcpee
Icon=` + PICPATH

	ioutil.WriteFile("/usr/share/applications/Kcpee.desktop", INIT)
	if _, err := os.Stat(DEKPATH); err == nil {
		if _, err := os.Stat(DEKPATH + "/Kcpee.desktop"); err != nil {
			yes, _ := dlgs.Question("Question", "if Create Desktop App icon?", true)
			if yes {
				ioutil.WriteFile("/usr/share/applications/Kcpee.desktop", INIT)
			}
		}
	}
}

func ProxySet(localAddr string) {
	if _, err := os.Stat("/usr/share/applications/Kcpee.desktop"); err != nil {
		InitDesktop()
	}
	if _, err := os.Stat("/etc/profile"); err != nil {
		return
	}
	RAWB, err := ioutil.ReadFile("/etc/profile")
	if err != nil {
		return
	}
	RAW := string(RAWB)
	if localAddr != "" {
		TMP := `
##### PROXY PROFILE START 
export http_proxy="` + localAddr + `"`
		TMP += `
export https_proxy="` + localAddr + `
##### PROXY PROFILE END`
		if !strings.Contains(RAW, TMP) {
			RAW += TMP
			out, err := exec.Command("bash", "-c", `sudo cat << EOF > /etc/profile\n`+RAW+"\nEOF").Output()
			if err != nil {
				dlgs.Error("Error Start Global Proxy", string(out)+err.Error())
			}
			GlobalStatus = true
			dlgs.Info("Infomation", "try to start proxy: "+localAddr)
		}
	} else {
		if strings.Contains(RAW, "##### PROXY PROFILE START") {
			s := strings.SplitN(RAW, "##### PROXY PROFILE START", 2)
			PRE := s[0]
			AFT := strings.TrimSpace(strings.SplitN(s[1], "##### PROXY PROFILE END", 2)[1])
			RAW = PRE + "\n" + AFT
			_, err := exec.Command("bash", "-c", `sudo cat << EOF > /etc/profile
`+RAW+"\nEOF").Output()
			if err != nil {
				dlgs.Error("Error Start Global Proxy", err.Error())
			}
			//ioutil.WriteFile("/etc/profile", []byte(RAW), os.ModePerm)
			GlobalStatus = false
			dlgs.Info("Infomation", "try to stop proxy: ")
		}
	}

}

func KillKcpee() {
	TMP := `ps aux| grep Kcpee |  egrep -v '(grep|egrep)' | awk '{ print $2}' | xargs kill -9 `
	exec.Command("bash", "-c", TMP).Output()
}
